package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.utils.DataCleanManager;
import com.diudiutechnology.www.yadiudiu.util.utils.Dialog;
import com.diudiutechnology.www.yadiudiu.util.utils.SharedPreferencesUtil;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.HomePageFragmentActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/17.
 * 设置
 */

public class SettingActivity extends Activity implements View.OnClickListener {
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.personal_data_layout)//个人资料
    RelativeLayout personalDataLayout;
    @Bind(R.id.with_regard_layout)//关于丫丢丢
   RelativeLayout with_regard_layout;

    @Bind(R.id.customer_service_center_layout)//客服中心
    RelativeLayout customer_service_center_layout;

    @Bind(R.id.give_score_layout)//给我们评分
     RelativeLayout give_score_layout;

    @Bind(R.id.clear_the_cache_layout)//清理缓存
            RelativeLayout clear_the_cache_layout;
    @Bind(R.id.log_outtext)//退出
            TextView log_outtext;
    @Bind(R.id.personal_data)//缓存
            TextView personal_data;

Context context;
    AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        personalDataLayout.setOnClickListener(this);
        with_regard_layout.setOnClickListener(this);
        give_score_layout.setOnClickListener(this);
        customer_service_center_layout.setOnClickListener(this);
        clear_the_cache_layout.setOnClickListener(this);
        log_outtext.setOnClickListener(this);

        try {
            personal_data.setText(DataCleanManager.getTotalCacheSize(this));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.personal_data_layout://个人资料
            startActivity(new Intent(context,PersonalDataActivity.class));
                break;
            case R.id.with_regard_layout://关于丫丢丢
                startActivity(new Intent(context,WithRegardActivity.class));
                break;
            case R.id.customer_service_center_layout://客服中心
                startActivity(new Intent(context,CustomerServiceCenterActivity.class));
                break;
            case R.id.give_score_layout://给我们评分
                Uri uri = Uri.parse("market://details?id="+getPackageName());
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.clear_the_cache_layout://清理缓存
                processCleanOut(0);
                break;
            case R.id.log_outtext://退出
                processCleanOut(1);
                break;
        }
    }
    private  void processCleanOut(final int pos){
        String tile="";
        String nei="";
        if (pos==0){
            tile=getResources().getString(R.string.clear_the_cache);
            nei=getResources().getString(R.string.tihuancun);
        }else {
            tile=getResources().getString(R.string.log_out);
            nei=getResources().getString(R.string.tuihuancun);
        }
        alertDialog= Dialog.phonedialog(context, nei, tile, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (pos==0){
                    DataCleanManager.clearAllCache(SettingActivity.this);
                    try {
                        personal_data.setText(DataCleanManager.getTotalCacheSize(SettingActivity.this));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else {
                    exit();
                }
                alertDialog.dismiss();
            }
        },"确定","取消");
    }
    public void exit() {

//        DataCleanManager.cleanInternalCache(getApplicationContext());
//        DataCleanManager.cleanDatabases(getApplicationContext());
//        DataCleanManager.cleanSharedPreference(getApplicationContext());
//        DataCleanManager.cleanFiles(getApplicationContext());
//        DataCleanManager.cleanExternalCache(getApplicationContext());

        SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.USERID_KEYS,"");//清空userid
        SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.TOKEN_KEYS,"");//清空token

        Toast.makeText(this, "退出成功", Toast.LENGTH_SHORT).show();
//        SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(this);
//        sp.edit().clear().commit();
        HomePageFragmentActivity homePageFragmentActivity=new HomePageFragmentActivity();
        homePageFragmentActivity.activity.processout();
        finish();
    }
}
